<template>
  <div id="app">
    <div class="pic">
      <img src="http://weldon.net.cn/back/img/logo2.fc048282.png" alt="" srcset="">
    </div>
    <div class="form">

      <el-form  ref="form" label-width="100px" class="demo-ruleForm" :rules="rules" :model="form">
        <el-form-item  prop="username">
          <el-input prefix-icon="el-icon-user-solid"  v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item  prop="password">
          <el-input type="password"   prefix-icon="el-icon-unlock"  v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item class="item">
          <el-button type="primary"  @click="submit">登录</el-button>
          <el-button type="info"  @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
</div>
</template>
<script>
import {login} from "@/api/user";
import {Message} from "element-ui";

export default {
  name:'Login',
  data() {
    return {
      form: {
        username: '',
        password: ''
      },
rules: {
        username:[
    { required: true, message: '请输入用户名', trigger: ['blur', 'change'] },
    { min: 5, max: 11, message: '长度在 5 到 11 个字符', trigger: ['blur', 'change'] }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: ['blur', 'change'] },
    { pattern: /^\w{5,11}$/, message: '请输入 5 到 10 位的密码', trigger: ['blur', 'change'] }
  ]
},

    };
  },
  methods: {
    async submit() {
 try{
   const res= await this.$refs.form.validate()
   // console.log(res)
   if(res) {
 const res1 =  await login(this.form)
     // console.log(res1.data.token)

     Message.success('登陆成功')
     this.$store.commit('user/setLogintoken',res1.data.token)
     this.$router.push('/welcome')


   }
 }catch(e) {
   console.log(e)
 }
    },
    reset(){
      this.$refs.form.resetFields()
    }
}
};


</script>
<style lang="less" >
* {
  margin: 0;
  padding: 0;
}
#app {
  background-color: #2b4b6b;
  display: flex;
  justify-content: center;
  align-items: center;
  .pic {
    width: 130px;
    height: 130px;
    position: relative;
    top: -180px;
    left: 280px;
    border: 2px #fff;
    //background-color: #fff;
  }
   .form{
    background-color: #fff;
    width: 450px;
    height: 300px;
    //margin: 300px auto;
    border: 1px solid #333;
    display: flex;
    justify-content: center;
    align-items: center;
    .demo-ruleForm{
      width: 500px;
      margin-left: -100px;
      margin-top: 80px;
    }
    .item{
      display: flex;
      justify-content: end;
    }
  }

  //background-color: #2b4b6b;
  height: 100vh
;
}
</style>
